package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.AssessmentInformation;
import tgc.edu.exam.web.form.AssessmentInformationForm;

@Controller
@RequestMapping(value="/assessmentInformation")
public class AssessmentInformationController extends CommonController<AssessmentInformation, UUID, AssessmentInformationForm> {
	@Override
	public Specification<AssessmentInformation> buildSpec(AssessmentInformationForm form) {
		Specification<AssessmentInformation> specification = new Specification<AssessmentInformation>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<AssessmentInformation> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(StringUtils.hasText(form.getExamination())) {
					Predicate name = cb.like(root.get("examination"), "%"+form.getExamination()+"%");
					rules.add(name);
				}
				if(StringUtils.hasText(form.getScoringmethod())) {
					Predicate difficultylevel = cb.like(root.get("scoringmethod"), "%"+form.getScoringmethod()+"%");
					rules.add(difficultylevel);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}
}
